layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * Warehouse Management
     */
    var BasicWarehouse = {
        tableId: "basicWarehouseTable"
    };

    /**
     * 初始化表格的列
     */
    BasicWarehouse.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {align: 'center',width: 120, toolbar: '#tableBar', title: '操作'},
            {field: 'warehouseOperateName', title: 'Project Department'},
            {field: 'warehouseName', title: 'Warehouse Name'},
            {field: 'warehouseTypes', title: 'Storage Type'},
            {field: 'warehouseType', title: 'Warehouse Type'},
            {field: 'warehouseNumber', title: 'Warehouse Number', sort: true},
            {field: 'warehouseContacts', title: 'Contact Person'},
            {field: 'tel', title: 'Contact Number'},
            {field: 'warehouseAddress', title: 'Detailed Address'},
            {field: 'longitude', title: 'Longitude'},
            {field: 'latitude', title: 'Latitude'},
            {field: 'warehouseCanmeasure', title: 'Available Area', sort: true},
            {field: 'warehouseMeasure', title: 'Total Area', sort: true},
            {field: 'warehouseNotes', title: 'Remarks'},
            {field: 'createTime', sort: true, title: 'Creation Time'},
            {field: 'warehouseXlbId', sort: true, title: 'XinLingBang Warehouse ID'},
        ]];
    };

    /**
     * Click Search Button
     */
    BasicWarehouse.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['warehouseOperateId'] = $("#warehouseOperateId").val();
        table.reload(BasicWarehouse.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * Open Add Dialog
     */
    BasicWarehouse.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/basicWarehouse/add_hx';
    };

    /**
     * Export Excel Button
     */
    BasicWarehouse.exportExcel = function () {
        var checkRows = table.checkStatus(BasicWarehouse.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * Click Edit
     *
     * @param data Row data when clicking the button
     */
    BasicWarehouse.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/basicWarehouse/edit_hx?id=' + data.id;
    };

    /**
     * Click Delete
     *
     * @param data Row data when clicking the button
     */
    BasicWarehouse.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/basicWarehouse/delete", function (data) {
                Feng.success("Delete successful!");
                table.reload(BasicWarehouse.tableId);
            }, function (data) {
                Feng.error("Delete failed!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure to delete?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicWarehouse.tableId,
        url: Feng.ctxPath + '/basicWarehouse/list',
        page: true,
        limit: 20,
        height: "full-60",
        cellMinWidth: 100,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        cols: BasicWarehouse.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicWarehouse.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BasicWarehouse.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicWarehouse.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BasicWarehouse.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BasicWarehouse.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BasicWarehouse.onDeleteItem(data);
        }
    });
});
